package com.jdd.pedicure.controller;

import com.jdd.pedicure.entity.Item;
import com.jdd.pedicure.entity.Result;
import com.jdd.pedicure.service.ItemService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RequestMapping("item")
@Controller
public class ItemController {
    @Resource
    ItemService itemService;

    @RequestMapping("goItem")
    public String goItem() {
        return "item";
    }

    @RequestMapping("/goAdd")
    public String addAdmin(Integer id, HttpServletRequest request) {
        Item byId = new Item();
        if (id != null) {
            byId = itemService.queryById(id);
        }
        request.setAttribute("edit", byId);
        return "itemAdd";
    }
    @ResponseBody
    @RequestMapping("/deletes")
    public Result<Item> doDelAdmins(@RequestBody List<Item> admins) {
        itemService.deletes(admins);
        Result<Item> itemResult = new Result<>();
        itemResult.setCode("0");
        return itemResult;
    }

    @ResponseBody
    @RequestMapping("/delete")
    public Result<Item> delete(int id) {
        itemService.delete(id);
        Result<Item> itemResult = new Result<>();
        itemResult.setCode("0");
        return itemResult;
    }

    @ResponseBody
    @RequestMapping("/save")
    public Result<Item> save(@RequestBody Item item) {
        itemService.saveOrUpdate(item);
        Result<Item> itemResult = new Result<>();
        itemResult.setCode("0");
        return itemResult;
    }

    @ResponseBody
    @RequestMapping("getData")
    public Result<Item> getData(Result<Item> result, Item example) {
        result.setExample(example);
        itemService.queryByPage(result);
        result.setCode("0");
        return result;
    }
}
